<template>
  <div class="app-container">
    <el-card shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="fileName" label="文件名称">
          <el-input
            v-model="searchData.fileName"
            placeholder="请输入"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-button
            type="primary"
            :icon="CirclePlus"
            @click="dialogVisible = true"
          >
            上传文件
          </el-button>
        </div>
        <div>
          <el-tooltip content="刷新当前页">
            <el-button
              type="primary"
              :icon="RefreshRight"
              circle
              @click="getTableData"
            />
          </el-tooltip>
        </div>
      </div>

      <div class="table-wrapper">
        <el-table :data="tableData" border highlight-current-row>
          <el-table-column
            align="center"
            label="序号"
            type="index"
            width="65"
          />
          <el-table-column prop="sign" label="文件唯一标志" align="center" />
          <el-table-column prop="filename" label="文件名" align="center" />
          <el-table-column prop="suffix" label="扩展名" align="center" />
          <el-table-column
            prop="size"
            label="文件大小（单位:KB）"
            align="center"
          />
          <el-table-column prop="createTime" label="上传日期" align="center" />
          <el-table-column
            fixed="right"
            label="操作"
            width="200"
            align="center"
          >
            <template #default="scope">
              <el-button link type="primary" @click="copyUrl(scope.row)">
                复制地址
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.limit"
          :currentPage="paginationData.page"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 上传 -->
    <FileUploadDialog v-if="dialogVisible" :closedDialog="handleClosedDialog" />
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from "vue";
import FileAPI, {
  type TableRequestParams,
  type FileVO,
} from "@/api/system/base/file";
import { type FormInstance } from "element-plus";
import { Search, CirclePlus, RefreshRight } from "@element-plus/icons-vue";
import { usePagination } from "@/hooks/usePagination";
import FileUploadDialog from "@/views/system/base/file/components/FileUploadDialog.vue";

/** 文件管理 */
defineOptions({
  name: "BaseFile",
});

/** 弹窗状态 */
const dialogVisible = ref<boolean>(false);
/** 表格状态 */
const loading = ref<boolean>(false);
/** 表格数据 */
const tableData = ref<FileVO[]>([]);

/** 查询表单 */
const searchFormRef = ref<FormInstance | null>(null);
/** 查询条件 */
const searchData = reactive<TableRequestParams>({
  fileName: "",
});
/** 翻页状态 */
const { paginationData, handleCurrentChange, handleSizeChange } =
  usePagination();

/** 关闭弹窗 */
const handleClosedDialog = () => {
  dialogVisible.value = false;
  getTableData();
};

/** 列表接口 */
const getTableData = () => {
  loading.value = true;
  FileAPI.getFileListApi({
    page: paginationData.page,
    limit: paginationData.limit,
    fileName: searchData.fileName || undefined,
  })
    .then((res) => {
      const { data } = res;
      paginationData.total = data.total;
      tableData.value = data.rows;
    })
    .catch(() => {
      tableData.value = [];
    })
    .finally(() => {
      loading.value = false;
    });
};
/** 复制文件地址 */
const copyUrl = (row: FileVO) => {
  if (row.sign) {
    navigator.clipboard
      .writeText(row.sign)
      .then(function () {
        ElMessage.success("复制成功");
      })
      .catch(function (err) {
        console.error("Failed to copy text: ", err);
      });
  }
};
/** 查询列表 */
const handleSearch = () => {
  paginationData.page === 1 ? getTableData() : (paginationData.page = 1);
};

/** 监听分页参数的变化 */
watch([() => paginationData.page, () => paginationData.limit], getTableData, {
  immediate: true,
});
</script>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;

  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
